/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ins.entity.InsDashboard;
import com.thinkgem.jeesite.modules.ins.service.InsDashboardService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 仪表盘Controller
 * @author fanqinglong
 * @version 2018-05-09
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insDashboard")
public class InsDashboardController extends BaseController {

	@Autowired
	private InsDashboardService insDashboardService;
	
	@ModelAttribute
	public InsDashboard get(@RequestParam(required=false) String id) {
		InsDashboard entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insDashboardService.get(id);
		}
		if (entity == null){
			entity = new InsDashboard();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = {"list", ""})
	public String list(InsDashboard insDashboard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InsDashboard> page = insDashboardService.findPage(new Page<InsDashboard>(request, response), insDashboard); 
		model.addAttribute("page", page);
		return "modules/ins/insDashboardList";
	}

	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = "form")
	public String form(InsDashboard insDashboard, Model model) {
		model.addAttribute("insDashboard", insDashboard);
		return "modules/ins/insDashboardForm";
	}

	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "save")
	public String save(InsDashboard insDashboard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, insDashboard)){
			return form(insDashboard, model);
		}
		insDashboard.setUser(UserUtils.getUser());
		insDashboardService.save(insDashboard);
		addMessage(redirectAttributes, "保存仪表盘成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insQuota/quotaIndex?repage";
	}
	
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "move")
	public String move(HttpServletRequest request,RedirectAttributes redirectAttributes){
		String id = request.getParameter("id");
		String parentId = request.getParameter("parent.id");
		InsDashboard insDashboard = new InsDashboard(id);
		insDashboard.setParent(new InsDashboard(parentId));
		insDashboardService.move(insDashboard);
		addMessage(redirectAttributes, "移动成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insQuota/quotaIndex?repage";
	}
	
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "delete")
	public String delete(InsDashboard insDashboard, RedirectAttributes redirectAttributes) {
		insDashboardService.delete(insDashboard);
		addMessage(redirectAttributes, "删除仪表盘成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insQuota/quotaIndex?repage";
	}

	/**
	 * 获取机构JSON数据。
	 * @param extId 排除的ID
	 * @param type	类型（1：公司；2：部门/小组/其它：3：用户）
	 * @param grade 显示级别
	 * @param response
	 * @return
	 */
	@RequiresPermissions("ins:insQuota:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, @RequestParam(required=false) String type,
			@RequestParam(required=false) Long grade, @RequestParam(required=false) Boolean isAll, HttpServletResponse response) {
		InsDashboard insDashboard = new InsDashboard();
		insDashboard.setUser(UserUtils.getUser());
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<InsDashboard> list = insDashboardService.findList(insDashboard);
		for (int i=0; i<list.size(); i++){
			InsDashboard e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("pIds", e.getParentIds());
			map.put("name", e.getName());
			if ("1".equals(e.getType())){
				map.put("isParent", true);
			}
			mapList.add(map);
		}
		return mapList;
	}
}